#include "BrickQuestion.h"
#include "Resource.h"


BrickQuestion::BrickQuestion(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State) : Object(x, y) //, ID, Object_State)
{
	this->setIDObjType(ID);
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\brick_32.png"), 125, 0);

	isMoveUp = false;

	if (this->getIDObjType() == 25)	//gach hoi
	{
		p_sprite->setFrame(2,4);
	} 
	else if (this->getIDObjType() == 26)	//gach an
	{
		p_sprite->setFrame(4,4);
	}
	else if (this->getIDObjType() == 28)	//gach sao
	{
		p_sprite->setFrame(0,0);
	}
	//else if (this->GetID() == 29)	//gach da va cham, luu tu map len
	//{
	//	_sprite->SetFrame(1,1);
	//	//_sprite->SetFrame(2,4);
	//	isMoveUp = true;
	//}

	_xOld = pos_x;
	_yOld = pos_y;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	veloc_x = 0;
	veloc_y = 0;


	//_accelY = 0;
	accel_y = 0;



	//obj_state = BS_NORMAL; //mac dinh object state la NORMAL:1
	this->obj_state = Object_State;
	if (obj_state == BS_NONE)
	{
		p_sprite->setFrame(1,1);
		isMoveUp = true;
	} 

}

BrickQuestion::~BrickQuestion()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}



void BrickQuestion::render(int cameraX, int cameraY)
{

	switch(obj_state)
	{
	case BS_NORMAL:
	case BS_NONE:
	case BS_MOVEUP:
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
		break;

	}


}


void BrickQuestion::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	switch(obj_state)
	{
	case BS_NORMAL:

		p_sprite->update(gameTime);
		break;

	case BS_MOVEUP:
		if (isMoveUp == false)
		{
			veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime + 1/2 * accel_y * gameTime * gameTime;
			

			//Xu li dung gach voi coin,...
			for (i = obj_list->begin(); i != obj_list->end(); i++)
			{
				obj = *i;


				//Box box = this->getBox(time);
				Box box(this->getPosX() + width/2,
					this->getPosY() - this->getHeight(), 
					this->getWidth()/2, 
					this->getHeight(), 
					0, 
					veloc_y * gameTime + 1.0f/2.0f * accel_y * gameTime * gameTime);

				//Box block = obj->getBox(time);
				Box block(obj->getPosX() + obj->getWidth()/2,
					obj->getPosY() - obj->getHeight(),// - 0.11,
					obj->getWidth()/2,
					obj->getHeight()
					);

				Box boxphase = Collision::GetSweptBroadphaseBox(box);
				if (Collision::AABBCheck(boxphase, block))
				{
					switch (obj->getIDObjType())
					{
					case 41:	//coin
					case 46:	//coin an
					case 47:	//5 coin hidden
						/*if (obj->getObjState() == IS_IDLE)
						{
						obj->changeState(IS_ACTIVING);
						obj->setIsChangeState(true);
						}*/
						//choi an gian nhe
						break;
					case 43:	//nam mang
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_ACTIVING);
						}
						break;
					case 45:	//nam to, hoa
						if (obj->getObjState() == IS_IDLE && obj->getIsActiveMushroom() == false)
						{
							obj->setIsActiveMushroom(true);
							//obj->changeState(IS_ACTIVING);
						}
						break;

					case 44:	//sao
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_ACTIVING);
						}
						break;

					case 11:		//goomba
					case 12:		//koopba
						obj->changeState(ES_FALL);
						break;


					}
					//if (obj->getIDObjType() == 41 ||		//coin hien
					//	obj->getIDObjType() == 46 ||		//coin an
					//	obj->getIDObjType() == 47)			//5coin an
					//{
					//	
					//}

					//if (obj->getIDObjType() == 43)
					//{
					//	
					//}
				}
			}

			if (pos_y <= _yOld)
			{
				changeState(BS_NONE);
			}
			p_sprite->update(gameTime);
		}
		break;

	case BS_NONE:
		p_sprite->update(gameTime);
		break;
	}

}

void BrickQuestion::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case BS_NORMAL:
		isMoveUp = true;
		pos_y = _yOld;
		veloc_y = 0;
		//_accelY = 0;
		accel_y = 0;
		break;
	case BS_MOVEUP:
		veloc_y = 1.0;
		//_accelY = GRAVITY;
		accel_y = GRAVITY;
		p_sprite->setFrame(1,1); //frame emptybox
		break;

	case BS_NONE:
		isMoveUp = true;
		pos_y = _yOld;
		veloc_y = 0;
		//_accelY = 0;
		accel_y = 0;
		break;

	case BS_REMOVE:
		break;
	}
}

OBJECT_TYPE BrickQuestion::getObjectType()
{
	return OBJECT_LAND;
}